package xj.graph2d;

import java.util.*;
import java.awt.geom.Rectangle2D;
import xj.util.gui.Rectangle2DFloat;

public class DrawShapeUtil { 

  public static Rectangle2D getShapeBounds(Collection<? extends DrawShape> shapes) {
    Rectangle2D bounds = null;
    if (shapes != null) {
      int n = shapes.size();
      if (n > 0) {
	boolean first = true;
	float xmin = 0, xmax = 0, ymin = 0, ymax = 0; 
	for (DrawShape shape : shapes) {
	  if (!shape.isVisible()) {
	    continue;
	  }
	  Rectangle2D r = shape.getBounds();
	  float x1 = (float) r.getX();
	  float y1 = (float) r.getY();
	  float x2 = x1 + (float) r.getWidth();
	  float y2 = y1 + (float) r.getHeight();
	  if (first) {
	    xmin = x1;
	    ymin = y1;
	    xmax = x2;
	    ymax = y2;
	    first = false;
	  } else {
	    xmin = Math.min(xmin, x1);
	    ymin = Math.min(ymin, y1);
	    xmax = Math.max(xmax, x2);
	    ymax = Math.max(ymax, y2);
	  }
	}
	bounds = new Rectangle2DFloat(xmin, ymin, (xmax - xmin), (ymax - ymin)); 
      }
    }
    return bounds;
  }
  
  public static void moveShapes(Collection<? extends DrawShape> shapes, float dx, float dy) {
    if (shapes != null) {
      for (DrawShape s : shapes) { 
	s.move(dx, dy);
      }
    }
  }

}